package com.yanqu.road.server.cmd.activity.sonhai;

import com.yanqu.road.entity.activity.sonhai.data.SonHaiRegionData;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiCreatePlotResp;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiGetPlotDetailResp;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiOccPowerAddInfo;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiPlotBuffResp;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.pb.SonHaiActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_SON_HAI_GET_PLOT_DETAIL, desc = "山海伏兽-获取地块详情")
public class CrossSonHaiGetPlotDetailCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SonHaiProto.SonHaiGetPlotDetailReqMsg reqMsg = SonHaiProto.SonHaiGetPlotDetailReqMsg.parseFrom(packet.getBytes());
        SonHaiProto.SonHaiGetPlotDetailRespMsg.Builder respMsg = SonHaiProto.SonHaiGetPlotDetailRespMsg.newBuilder();
        CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_SON_HAI_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_GET_PLOT_DETAIL, respMsg));
            return;
        }
        //获取地块详情
        try {
            //获取地块详情
            SonHaiGetPlotDetailResp resp = activity.getPlotDetail(packet.getPlayerId(), reqMsg.getRegionId(), reqMsg.getX(), reqMsg.getY());
            //获取玩家
            CrossSonHaiUser user = activity.getUser(packet.getPlayerId());
            CrossSonHaiTeam team = activity.getTeam(user.getTeamId());
            CrossSonHaiUser targetUser = null;
            if (resp.getUserData() != null) {
                targetUser = activity.getUser(resp.getUserData().getUserId());
            }
            //区域
            SonHaiRegionData regionData = resp.getRegionData();
            //构造PB
            SonHaiProto.SonHaiPlotData.Builder builder = SonHaiActivityPb.buildSonHaiPlotData(user, regionData, resp.getPlotData(), resp.getBeastBossData(), targetUser, resp.getEggData());
            respMsg.setRet(0);
            respMsg.setPlotData(builder);
            respMsg.setPvePower(user.getPowerForPve(regionData));
            respMsg.setPvpPower(user.getPowerForPvp());
            respMsg.setPowerAddRate(resp.getPowerAddRate());
            respMsg.setPowerAddRateForNormal(resp.getPowerAddRateForNormal());
            respMsg.setPowerAddRateFroOnlyPve(resp.getPowerAddRateFroOnlyPve());
            //协助的队友
            for (CrossSonHaiUser member : team.getMemberMap().values()) {
                if (member.getUserId() != packet.getPlayerId()) {
                    respMsg.addHelpUsers(SonHaiActivityPb.buildSonHaiHelpUserData(member, regionData));
                }
            }
            respMsg.setScoreAddRate(user.getScoreAddRate(regionData));
            respMsg.setProgressAddRate(user.getProgressOpRateNotCondition(regionData));
            respMsg.setHelpCdEndTime(resp.getHelpCdEndTime());
            for (SonHaiPlotBuffResp info : resp.getBeastPowerAddPlots()) {
                respMsg.addBeastPowerAddRatePlots(SonHaiActivityPb.buildSonHaiPlotBuffData(info));
            }
            for (SonHaiPlotBuffResp info : resp.getScoreRePlots()) {
                respMsg.addScoreReRatePlots(SonHaiActivityPb.buildSonHaiPlotBuffData(info));
            }
            for (SonHaiPlotBuffResp info : resp.getProgressRePlots()) {
                respMsg.addProgressReValuePlots(SonHaiActivityPb.buildSonHaiPlotBuffData(info));
            }
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_GET_PLOT_DETAIL, respMsg));
    }

    @Override
    public void sendData() {

    }
}
